const errorType = require('../constants/error-types');

const errorHandler = (error, ctx) => {
  let status, message;
  switch (error.message) {
    case errorType.ACCOUNT_OR_PASSWORD_IS_REQUIRED:
      status = 400;
      message = '密码或账号不能为空';
      break;
    case errorType.NAME_OR_PASSWORD_OR_ACCOUNT_IS_REQUIRED:
      status = 400;
      message = '用户名或者密码或账号不能为空';
      break;
    case errorType.ACCOUNT_NUMBER_ALREADY_EXISTS:
      status = 409;
      message = '账号已存在';
      break;
    case errorType.USER_DOES_NOT_EXISTS:
      status = 400;
      message = '账号不存在';
      break;
    case errorType.WRONG_PASSWORD:
      status = 400;
      message = '密码错误';
      break;
    case errorType.UNAUTHORIZATION:
      status = 401;
      message = '用户未授权';
      break;
    default:
      status = 400;
      message: 'NOT FOUND';
      break;
  }
  ctx.status = status;
  ctx.body = message
};

module.exports = errorHandler;
